(function () {

    "use strict";

    var api = {
        "addrListsUrl": '/api/address/addrLists',
        "setDefaultUrl": '/api/address/setDefault',
        "deleteAddrUrl": '/api/address/delete',
    };
    sm.controller('wx-center-address-index-controller', ['$scope', '$http', 'Modal', 'Toast', '$location', function ($scope, $http, Modal, Toast, $location){

        $scope.addrLists = [];

        function formatAddr(addrLists) {
            angular.forEach(addrLists, function (val) {
                $scope.addrLists.push({
                    addressId: val.addressId,
                    name: val.name,
                    phone: val.phone,
                    detail: val.province + ' ' + val.city + ' ' + val.area + ' ' + val.address,
                    default: val.default
                });
            });
        }

        $http.get(api.addrListsUrl).success(function (res) {
            if (res.state) {
                formatAddr(res.data);
            }
        });

        $scope.setDefault = function (aId, index) {
            if (!aId) {
                return false;
            }

            $http.post(api.setDefaultUrl, {
                addressId: aId
            }).success(function (res) {
                if (res.state) {
                    angular.forEach($scope.addrLists, function (val, key) {
                        val['default'] = false;
                        if (key == index) {
                            val['default'] = true;
                        }
                    });

                    Toast.success({
                        title: '修改成功'
                    });
                } else {
                    Toast.success({
                        title: '修改失败'
                    });
                }
            });
        };

        $scope.edit = function (addr, index) {

            if (!addr || !addr.addressId) {
                return false;
            }

            $location.search({id: addr.addressId});
            $location.path('/address/edit');


        };

        // 删除
        $scope.trash = function (addr, index) {

            if (!addr || !addr.addressId) {
                return false;
            }

            Modal.alert({
                'title': '操作提示',
                'content': '您正在删除收货地址 删除将不能恢复 请谨慎操作!',
                'buttons': [{
                    name: '取消'
                }, {
                    name: '确认删除',
                    event: function () {
                        console.log('here');
                        deleteAddr(addr.addressId, index);
                    }
                }]
            });

            function deleteAddr(addrId, index) {
                $http.get(api.deleteAddrUrl, {
                    params: {
                        addressId: addrId
                    }
                }).success(function (res) {
                    if (res.state) {
                        Toast.success({title: '删除成功'});
                        $scope.addrLists.splice(index, 1);
                    } else {
                        Toast.error({title: '删除失败'});
                    }
                }).error(function (res) {
                    Toast.error({title: '删除失败'});
                });
            }


        };

    }]);
})();

